package com.cjm.springmvc;

import com.cjm.springmvc.entities.Master;
import com.cjm.springmvc.entities.Product;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;

/**
 * Created by jiaming.chen1 on 2015/7/3.
 */
@Controller
public class HelloWorldTest {



    @RequestMapping(value = "/testModelAttribute")
    public String testModelAttribute(Product product){
        System.out.println(product);
        return "success";
    }

    @RequestMapping(value = "/testSessionAttribute")
    public String testSessionAttribute(Map<String, Object> map){
        System.out.println(map.getClass().getName());
        map.put("names", Arrays.asList("ni", "wo", "ta"));
        return "success";
    }

    @RequestMapping(value = "/testMap")
    public String testMap(Map<String, Object> map){
        System.out.println(map.getClass().getName());
        map.put("names", Arrays.asList("ni", "wo", "ta"));
        return "success";
    }


    @RequestMapping(value = "/testModelAndView")
    public ModelAndView testModelAndView(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("success");
        System.out.println("testModelAndView");
        return modelAndView;
    }


    @RequestMapping(value = "/testServletAPI")
    public String testServletAPI(HttpServletRequest request, HttpServletResponse response){
        System.out.println(request);
        System.out.println(response);
        return "success";
    }


    @RequestMapping("/testPOJO")
    public String testPOJO(Master master){
        System.out.println(master.toString());
        return "success";
    }


    @RequestMapping(value = "/testCookieValue")
    public String testCookieValue(@CookieValue(value = "JSESSIONID") String cookie){
        System.out.println(cookie);
        return "success";
    }

    @RequestMapping(value = "/testRequestParam")
    public String testRequestParam(@RequestParam(value = "username") String username, @RequestParam(value = "age",required = false, defaultValue = "0") Integer age){
        System.out.println("username: "+username+" age: "+age);
        return "success";
    }


    @RequestMapping(value = "/testMethodPUT/{id}", method = RequestMethod.PUT)
    public String testMethodPUT(@PathVariable(value = "id") Integer id){
        System.out.println("testMethodPUT: "+id);
        return "success";
    }

    @RequestMapping(value = "/testMethodDELETE/{id}", method = RequestMethod.DELETE)
    public String testMethodDELETE(@PathVariable(value = "id") Integer id){
        System.out.println("testMethodDELETE: "+id);
        return "success";
    }

    @RequestMapping(value = "/testMethodPOST/{id}", method = RequestMethod.POST)
    public String testMethodPOST(@PathVariable(value = "id") Integer id){
        System.out.println("testMethodPOST: "+id);
        return "success";
    }

    /**
     * 使用REST.GET
     * @return
     */
    @RequestMapping(value = "/testMethodGET", method = RequestMethod.GET)
    public String testMethodGET(){
        System.out.println("testMethodGET");
        return "success";
    }


    @RequestMapping(value = "/helloWorld")
    public String helloWorld(){
        return "success";
    }


}
